<?php
/**
 * Template Name: Register Form
*/

global $theme; get_header(); ?>

    <div id="main">
    
        <?php $theme->hook('main_before'); ?>

        <div id="content">
            
            <?php $theme->hook('content_before'); ?>
        
            <?php 
                if (have_posts()) : while (have_posts()) : the_post();
                    /**
                     * Find the post formatting for the pages in the post-page.php file
                     */
                    get_template_part('post', 'page');
                endwhile;
                
                else :
                    get_template_part('post', 'noresults');
                endif; 
            ?>
            
            <div class="contact-form">
                <?php
                if (isset($_POST['register_form_submit'])) {

                    if (!$_POST['register_form_name'] 
                            || !$_POST['register_form_email'] 
                            || !$_POST['register_form_edad'] 
                            || $_POST['register_form_iglesia'] == 'Seleccione una iglesia...'
                            || !$_POST['register_form_telefono']) {
                        ?><div class="error">Por favor verifique que todos los campos fueron llenados correctamente</div><?php
                    } elseif (!is_email($_POST['register_form_email'])) {
                        ?><div class="error">Email inv&aacute;lido!</div><?php
                    }  else {
                        if ($wpdb->insert('participants', array(
                            'nombre' => $_POST['register_form_name'],
                            'mail' => $_POST['register_form_email'],
                            'edad' => $_POST['register_form_edad'],
                            'iglesia' => $_POST['register_form_iglesia'],
                            'telefono' => $_POST['register_form_telefono']
                        ))) {
                            $registred = true;
                            ?><div class="registered-msg">Gracias por registrarte! Te mantendremos informado por email sobre las novedades del campamento.</div><?php
                        } else {
                            ?><div class="error">Error. Por favor comunique al administrador.</div><?php
                        }
                    }
                }
                ?>
                
                <?php if (!$registred) : ?>
                    <form id="register-form" method="post" action="">
                        <input type="hidden" name="register_form_submit" value="true" />
                        <div class="contact-form-label alignleft <?php if($_POST && !$_POST['register_form_name']) { echo 'contact-form-required'; } ?>">Nombre:</div>
                        <div class="contact-form-input"><input type="text" name="register_form_name" value="<?php if ( isset($_POST['register_form_name']) ) { echo esc_attr($_POST['register_form_name']); } ?>" /></div>
                        
                        <div class="contact-form-label alignleft <?php if($_POST && !$_POST['register_form_email']) { echo 'contact-form-required'; } ?>">Email:</div>
                        <div class="contact-form-input"><input type="text" name="register_form_email" value="<?php if ( isset($_POST['register_form_email']) ) { echo esc_attr($_POST['register_form_email']); } ?>" /></div>
                        
                        <div class="contact-form-label alignleft <?php if($_POST && !$_POST['register_form_edad']) { echo 'contact-form-required'; } ?>">Edad:</div>
                        <div class="contact-form-input"><input type="text" name="register_form_edad" value="<?php if ( isset($_POST['register_form_edad']) ) { echo esc_attr($_POST['register_form_edad']); } ?>" /></div>
                        
                        <div class="contact-form-label alignleft <?php if($_POST && $_POST['register_form_iglesia'] == 'Seleccione una iglesia...') { echo 'contact-form-required'; } ?>">Iglesia:</div>
                        <div class="contact-form-input">
                            <select name="register_form_iglesia">
                                <option>Seleccione una iglesia...</option>
                                <option <?php if ($_POST && $_POST['register_form_iglesia'] == 'Comunidad y Servicios Cristianos (Central)') { echo 'selected="selected"'; }?>>
                                    Comunidad y Servicios Cristianos (Central)
                                </option>
                                <option <?php if ($_POST && $_POST['register_form_iglesia'] == 'Comunidad y Servicios Cristianos Los Lotes') { echo 'selected="selected"'; }?>>
                                    Comunidad y Servicios Cristianos Los Lotes
                                </option>
                                <option <?php if ($_POST && $_POST['register_form_iglesia'] == 'Capilla Cochabamba') { echo 'selected="selected"'; }?>>
                                    Capilla Cochabamba
                                </option>
                                <option <?php if ($_POST && $_POST['register_form_iglesia'] == '20 de Mayo') { echo 'selected="selected"'; }?>>
                                    20 de Mayo
                                </option>
                                <option <?php if ($_POST && $_POST['register_form_iglesia'] == 'Jireh') { echo 'selected="selected"'; }?>>
                                    Jireh
                                </option>
                                <option <?php if ($_POST && $_POST['register_form_iglesia'] == 'Cena de Senor') { echo 'selected="selected"'; }?> value="Cena de Senor">
                                    Cena de Se&ntilde;or
                                </option>
                            </select>
                        </div>

                        <div class="contact-form-label alignleft <?php if($_POST && !$_POST['register_form_telefono']) { echo 'contact-form-required'; } ?>">Tef&eacute;fono:</div>
                        <div class="contact-form-input"><input type="text" name="register_form_telefono" value="<?php if ( isset($_POST['register_form_telefono']) ) { echo esc_attr($_POST['register_form_telefono']); } ?>" /></div>
                        
                        <div class="contact-form-label alignleft">&nbsp;</div>
                        <div class="contact-form-input"><input type="submit" value="<?php _e('Registrarme','themater'); ?>" /></div>
                    </form>
                <?php endif; ?>
                
            </div>
            
            <?php $theme->hook('content_after'); ?>
        
        </div><!-- #content -->
    
        <?php #get_sidebars(); ?>
        
        <?php $theme->hook('main_after'); ?>
        
    </div><!-- #main -->
    
<?php get_footer(); ?>